package com.exam.userms.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.Toast;

import com.exam.userms.R;
import com.exam.userms.UserData;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

/**
 * 用户适配器
 * Created by guopei on 2020/6/28
 */
public class UserAdapter extends BaseAdapter {

    private LinkedList<UserData> users;
    private Context mContext;

    /**
     * 构造器
     *
     * @param users
     * @param mContext
     */
    public UserAdapter(LinkedList<UserData> users, Context mContext) {
        this.users = users;
        this.mContext = mContext;
    }

    @Override
    public int getCount() {
        return users.size();
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final int index = position;
        ViewHolder holder = null;

        // 复用 ConvertView ,提高性能
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.list_item, parent, false);
            holder = new ViewHolder();


            holder.name = (TextView) convertView.findViewById(R.id.name);
            holder.phone = (TextView) convertView.findViewById(R.id.phone);
            holder.checked = (CheckBox) convertView.findViewById(R.id.checked);
            convertView.setTag(holder);   //将Holder存储到convertView中
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.name.setText(users.get(index).getName());
        holder.phone.setText(users.get(index).getPhone());

        holder.checked.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                users.get(index).setChecked(isChecked);
            }
        });
        holder.checked.setChecked(users.get(index).getChecked());

        return convertView;
    }

    /**
     * ViewHolder 重用组件
     */
    public static class ViewHolder {
        TextView name;
        TextView phone;
        CheckBox checked;
    }

    //删除选中的数据
    public List<Integer> deletingData() {
        List<Integer> ids = new ArrayList<>();
        int y = 0;
        Iterator<UserData> iterator = users.listIterator();
        while (iterator.hasNext()) {
            UserData next = iterator.next();
            if (next != null && next.getChecked()) {
                ids.add(next.getId());
                iterator.remove();
                y++;
            }
        }

        notifyDataSetChanged();

        if (y == 0) {
            Toast.makeText(mContext, "请先选择用户", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(mContext, "已删除"+ ids.size() +"个用户", Toast.LENGTH_SHORT).show();
        }
        return ids;
    }

    public void send() {
        List<Integer> ids = new ArrayList<>();
        int y = 0;
        Iterator<UserData> iterator = users.listIterator();
        while (iterator.hasNext()) {
            UserData next = iterator.next();
            if (next != null && next.getChecked()) {
                ids.add(next.getId());
                y++;
            }
        }

        notifyDataSetChanged();

        if (y == 0) {
            Toast.makeText(mContext, "请先选择用户", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(mContext, "已群发" + ids.size() +"个用户", Toast.LENGTH_SHORT).show();
        }
    }

    public void clear() {
        List<Integer> ids = new ArrayList<>();
        int y = 0;
        Iterator<UserData> iterator = users.listIterator();
        while (iterator.hasNext()) {
            UserData next = iterator.next();
            if (next != null && next.getChecked()) {
                next.setChecked(false);
                y++;
            }
        }

        notifyDataSetChanged();

        if (y == 0) {
            Toast.makeText(mContext, "已清除", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(mContext, "已设置全部未勾选", Toast.LENGTH_SHORT).show();
        }
    }
}